
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


driver = webdriver.Edge(executable_path='D:\webdriver_驱动\msedgedriver.exe')
driver.maximize_window()
driver.get('https://www.douban.com/')
# 隐式等待
driver.implicitly_wait(10)
# 进入frame框架
f = driver.find_element(By.CSS_SELECTOR, '.login > iframe:nth-child(1)')
driver.switch_to.frame(f)
time.sleep(2)
# 输入手机号
driver.find_element(By.CLASS_NAME, 'account-form-input').send_keys('15985645480')
# 强制等待30秒，输入验证码
time.sleep(30)
# 点击登录豆瓣
driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
time.sleep(2)
# 退出frame框架
driver.switch_to.default_content()
time.sleep(2)
# 点击写日记
rj = driver.find_element(By.XPATH, '//a[@href="https://www.douban.com/note/create"]')
rj.click()
print(f'文本值：{rj.text}')
print(f'元素的具体class属性值：{rj.get_attribute("target")}')
print(f'元素的宽高：{rj.size}')
print(f'元素的坐标值：{rj.location}')
print(f'元素是否可用：{rj.is_enabled()}')
print(f'元素是否显示：{rj.is_displayed()}')
print(f'元素是否选中：{rj.is_selected()}')
time.sleep(2)
try:
    print('s')
except Exception as e:
    print(e)

# 窗口切换
driver.switch_to.window(driver.window_handles[-1])
time.sleep(2)
# 输入标题
bt = driver.find_element(By.CLASS_NAME, 'editor-input')
bt.send_keys('day11')
time.sleep(2)
# 鼠标右击
ActionChains(driver).context_click(bt).perform()
time.sleep(2)
#鼠标三击事件
ActionChains(driver).double_click(bt).click().perform()
time.sleep(2)
# 键盘复制事件
bt.send_keys(Keys.CONTROL, 'c')
time.sleep(2)
# 输入正文
zw = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/div/div[2]/div/div[2]/div')
zw.click()
time.sleep(1)
# 键盘粘贴事件
zw.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
# 键盘回车事件
zw.send_keys(Keys.ENTER)
time.sleep(2)
# 键盘输入事件
zw.send_keys('今天2025、05、05写日记')
time.sleep(2)
# 点击下一步
driver.find_element(By.CLASS_NAME, 'editor-extra-button-submit').click()
time.sleep(2)
# 点击权限设置
driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[3]/div/a').click()
time.sleep(2)
# 选择隐私设置
set1 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[3]/div[2]/div[1]/div[2]/select'))
set1.select_by_value('S')
# set1.select_by_visible_text('仅朋友可见')
time.sleep(2)
# 选择回应设置
set2 = Select(driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/form/div[3]/div[2]/div[2]/div[2]/select'))
set2.select_by_index(1)
# 强制等待10秒，输入验证码
time.sleep(10)
# 点击提交
driver.find_element(By.CSS_SELECTOR, '.editor-popup-setting-submit > button:nth-child(2)').click()
time.sleep(2)
# 点击删除
driver.find_element(By.CSS_SELECTOR, '.j').click()
time.sleep(2)
# 弹窗确定
alert = driver.switch_to.alert
alert.accept()
time.sleep(10)
















